package app.base.systemmanage.systemusersmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systemusersmanage.dao.SysUsersDAO;

public class SystemUsersFlatQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		int start = Integer.parseInt(URLDecoder.decode(request.getParameter("start"), Utility.CHARSET_ENCODING_UTF8));
	    int limit = Integer.parseInt(URLDecoder.decode(request.getParameter("limit"), Utility.CHARSET_ENCODING_UTF8));
	    
	    String cmd = request.getParameter("queryCmd");
	    
		String result = null;

		if (cmd == null || cmd.trim().length() == 0)
		{
			result = "{returnCode: '-1', msg: '客户端的数据请求命令参数CMD为设置错误'}";
		}
		else
		{
			cmd = URLDecoder.decode(cmd, Utility.CHARSET_ENCODING_UTF8).trim();
		
			String loginId = request.getParameter("loginId");
			String userName = request.getParameter("userName");
			String state = request.getParameter("state");
			
			loginId = (loginId == null || loginId.trim().length() == 0) ? "%" : URLDecoder.decode(loginId, Utility.CHARSET_ENCODING_UTF8).trim();
			userName = (userName == null || userName.trim().length() == 0) ? "%" : URLDecoder.decode(userName, Utility.CHARSET_ENCODING_UTF8).trim();
	    	state = (state == null || state.trim().length() == 0 || state.trim().equals("-1")) ? "%" : URLDecoder.decode(state, Utility.CHARSET_ENCODING_UTF8);
			
		    Map<String, Object> map = new HashMap<String, Object>();
		    
			map.put("start", BigDecimal.valueOf(start + 1));
			map.put("end", BigDecimal.valueOf(start + limit));
			map.put("cmd", cmd);
			map.put("loginId", loginId);
			map.put("userName", userName);
			map.put("state", state);
			
			result = getGridListData(map).toString();
		}
		
	    out.println(result);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	@SuppressWarnings("unchecked")
	public StringBuffer getGridListData(Map<String, Object>  map) 
		throws UnsupportedEncodingException
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysUsersDAO sysUsersDAO = (SysUsersDAO)context.getBean(Utility.SYSTEM_USERS_MANAGE_MODULE_BEAN_NAME);

		StringBuffer json = new StringBuffer("{totalProperty:" + sysUsersDAO.count_0001(map) + ",root:[");
		
		List list = sysUsersDAO.select_0001(map);	

		for(int index = 0; index < list.size(); index++){
			Map resultMap = (Map) list.get(index);
            
            json.append("{" 
            	+ "userId:'" + resultMap.get("USER_ID").toString().trim()
             	+ "',loginId:'" + resultMap.get("LOGIN_ID").toString().trim() 
             	+ "',userName:'" + resultMap.get("USER_NAME").toString().trim() 
             	+ "',userDesc:'" + (resultMap.get("USER_DESC") == null ? "" : resultMap.get("USER_DESC").toString().trim())
            	+ "',userTypeId:'" + resultMap.get("USER_TYPE_ID").toString().trim()
            	+ "',userTypeName:'" + resultMap.get("USER_TYPE_NAME").toString().trim()
            	+ "',lastLoginIp:'" + (resultMap.get("LAST_LOGIN_IP") == null ? "" : resultMap.get("LAST_LOGIN_IP").toString().trim())
            	+ "',lastLoginTime:'" + (resultMap.get("LAST_LOGIN_TIME") == null ? "" : resultMap.get("LAST_LOGIN_TIME").toString().trim())
            	+ "',lastPwdModifyTime:'" + (resultMap.get("LAST_PWD_MODIFY_TIME") == null ? "" : resultMap.get("LAST_PWD_MODIFY_TIME").toString().trim())
            	+ "',displayRankingIndex:'" + resultMap.get("DISPLAY_RANKING_INDEX").toString().toString().trim()
            	+ "',sysHoldFlag:'" + resultMap.get("SYS_HOLD_FLAG").toString().trim()
            	+ "',state:'" + resultMap.get("STATE").toString().trim()
            	+ "'}");
            	            
            if (index < list.size() - 1) {
            	json.append(",");
            }
		}
		
		json.append("]}");
		
		return json;
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		

	}

}